#include "KeyboardHandler.h"
#include <algorithm>
#include <QKeyEvent>
#include <QtAlgorithms>


/*****************************
* This class was made to test the signals and slots
* in Desk and MainPanel if a Kinect was not available.
* It's pretty cumbersome to use as a controller, but
* it works.  To use, make sure to comment out the necessary
* lines in devicemanager.h/.cpp and comment in others.
*
******************************/

KeyboardHandler::KeyboardHandler():
        m_current(NULL)
{
}

void KeyboardHandler::slotKeyStroke(int key)
 {
	 //Add an input point and connect device.
     if (key == Qt::Key_Equal) {
		 emit signalDeviceConnected();
         addInputPoint();
     } 
	 //Remove input point
	 else if (key == Qt::Key_Minus){
         removeInputPoint();
     } 
	 //Move left
	 else if (key == Qt::Key_A){
         update(m_current, -.05, 0, 0);
     } 
	 //Move Right
	 else if (key == Qt::Key_D){
         update(m_current, .05,0, 0);
     } 
	 //Move Up
	 else if (key == Qt::Key_W){
         update(m_current, 0,.05, 0);
     } 
	 //Move Down
	 else if (key == Qt::Key_S){
         update(m_current, 0,-.05, 0);
     } 
	 //Switch control of cursor
	 else if (key == Qt::Key_BracketLeft){
         changeFocusPrimary();
     } 
	 else if (key == Qt::Key_BracketRight){
         changeFocusSecondary();
     } 
	 //Select picture
	 else if (key == Qt::Key_Space) {
         select(m_current);
     }
	 //Deselect it
     else if (key == Qt::Key_Shift) {
         deselect(m_current);
     }
	 else if (key == Qt::Key_L){
		 emit signalSaveState();
	 }
	 else if (key == Qt::Key_K){
		 emit signalUnsave();
	 }
	 else if (key == Qt::Key_1){
		 emit signalScale(1.2);
	 }
	 else if (key == Qt::Key_2){
		 emit signalScale(.8);
	 }
	 else if (key == Qt::Key_3){
		 emit signalRotate(30, Qt::ZAxis);
	 }
	 else if (key == Qt::Key_4){
		 emit signalRotate(-30, Qt::ZAxis);
	 }
	 else if (key == Qt::Key_5){
		 emit signalRotate(30, Qt::XAxis);
	 }
	 else if (key == Qt::Key_6){
		 emit signalRotate(-30, Qt::XAxis);
	 }
	 else if (key == Qt::Key_7){
		 emit signalRotate(30, Qt::YAxis);
	 }
	 else if (key == Qt::Key_8){
		 emit signalRotate(-30, Qt::YAxis);
	 }
     else if (key == Qt::Key_0) {
         // swipe import/load menu right
         emit signalBrowseImportMenu(1);
		 emit signalBrowseLoadMenu(1);
     }
     else if (key == Qt::Key_9) {
         // swipe import/load menu left
		 emit signalBrowseLoadMenu(-1);
         emit signalBrowseImportMenu(-1);
     }

 }

void KeyboardHandler::addInputPoint()
{
	if(!m_inputs.count(1)){
		m_inputs.insert(std::pair<int, InputPoint>(1, InputPoint(0,0,0,1)));
		m_current = 1;
	}else if (!m_inputs.count(2)){
		m_inputs.insert(std::pair<int, InputPoint>(2, InputPoint(0,0,0,2)));
		m_current = 2;
	}

    emit signalAddedInput(m_inputs[m_current]);
}

void KeyboardHandler::removeInputPoint()
{
	if (m_inputs.count(2)){
        m_inputs.erase(2);
        emit signalRemovedInput(2, false);
		m_current = 1;
    }else if (m_inputs.count(1)){
        m_inputs.erase(1);
        emit signalRemovedInput(1, false);
		m_current = 0;
    }
}

void KeyboardHandler::update(int id, double dx, double dy, double dz)
{
	if (m_inputs[1].state() != FREE_MOTION || m_inputs[2].state() != FREE_MOTION){
		emit signalMove(dx, dy);
	}
        m_inputs[id].setX(m_inputs.at(id).x()+dx);
        m_inputs[id].setY(m_inputs.at(id).y()+dy);
        m_inputs[id].setZ(m_inputs.at(id).z()+dz);

        emit signalUpdateCursor(m_inputs[id]);
}


void KeyboardHandler::select(int id) {
    InputPoint p = m_inputs.at(id);
    emit signalSelect(p);
}

void KeyboardHandler::deselect(int id) {
    InputPoint p = m_inputs.at(id);
    emit signalDeselect(p);
}


void KeyboardHandler::changeFocusPrimary()
{
	if (m_inputs.count(1)){
		m_current = 1;
	}
}

void KeyboardHandler::changeFocusSecondary()
{
	if (m_inputs.count(2)){
		m_current = 2;
	}
}

void KeyboardHandler::slotUpdateInputPoint(int id, InputState state){
	if(id == 1) {
		m_inputs[1].setState(state);
	}
	else if(id == 2) {
		m_inputs[2].setState(state);
	}
	else {}
}
